package com.kbbywyclient.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ViewUtils;


/**
 * Created by FHW
 * on 2017/3/28
 */
public class CustomDialog extends Dialog {
    private TextView tv_title;
    private TextView tv_content;
    private Button btn_left;
    private Button btn_right;

    private Context context;
    private ButtonListener buttonListener;
    private String title = "";
    private String content = "";
    private String leftStr = "";
    private String rightStr = "";

    private CustomDialog(Context context) {
        super(context);
        this.context = context;
    }

    public CustomDialog(Context context, int theme, String title, String string, ButtonListener buttonListener) {
        super(context, theme);
        this.context = context;
        this.buttonListener = buttonListener;
        this.title = title;
    }

    public CustomDialog(Context context, int theme, String title, String content, ButtonListener buttonListener, String leftStr, String rightStr) {
        super(context, theme);
        this.context = context;
        this.buttonListener = buttonListener;
        this.title = title;
        this.content = content;
        this.leftStr = leftStr;
        this.rightStr = rightStr;
    }

    public CustomDialog(Context context, int theme, int title, String content, ButtonListener buttonListener, String leftStr, String rightStr) {
        super(context, theme);
        this.context = context;
        this.buttonListener = buttonListener;
        this.title = context.getString(title);
        this.content = content;
        this.leftStr = leftStr;
        this.rightStr = rightStr;
    }

    public CustomDialog(Context context, int theme, int title, int content, ButtonListener buttonListener, String leftStr, String rightStr) {
        super(context, theme);
        this.context = context;
        this.buttonListener = buttonListener;
        this.title = context.getString(title);
        this.content = context.getString(content);
        this.leftStr = leftStr;
        this.rightStr = rightStr;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom);

        initView();
    }

    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_content = (TextView) findViewById(R.id.tv_content);
        btn_left = (Button) findViewById(R.id.btn_left);
        btn_right = (Button) findViewById(R.id.btn_right);
        btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonListener.left();
            }
        });
        btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonListener.right();
            }
        });

        if (StringUtil.isNotEmpty(title)) {
            ViewUtils.setText(tv_title, title);
            tv_title.setVisibility(View.VISIBLE);
        } else {
            tv_title.setVisibility(View.GONE);
        }

        if (StringUtil.isNotEmpty(content)) {
            ViewUtils.setText(tv_content, content);
            tv_content.setVisibility(View.VISIBLE);
        } else {
            tv_content.setVisibility(View.INVISIBLE);
        }

        if (StringUtil.isNotEmpty(leftStr)) {
            btn_left.setText(leftStr);
        }
        if (StringUtil.isNotEmpty(rightStr)) {
            btn_right.setText(rightStr);
        }
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        this.title = title;
        ViewUtils.setText(tv_title, title);
    }

    /**
     * 设置标题
     *
     * @param titleRes
     */
    public void setTitle(int titleRes) {
        this.title = context.getString(titleRes);
        ViewUtils.setText(tv_title, title);
    }

    /**
     * 设置内容
     *
     * @param content
     */
    public void setContent(String content) {
        this.content = content;
        ViewUtils.setText(tv_content, content);
    }

    /**
     * 设置内容
     *
     * @param contentRes
     */
    public void setContent(int contentRes) {
        this.content = context.getString(contentRes);
        ViewUtils.setText(tv_content, contentRes);
    }

    public interface ButtonListener {
        public void left();

        public void right();
    }


}